package com.cg.design.graphicdesignpattern.facade.pagemaker;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

/**
 * 根据邮件地址使用Datebase类和HtmlWriter类编写该用户的Web页面
 *
 * @program: LeetCode->PageMaker
 * @description: 根据邮件地址使用Datebase类和HtmlWriter类编写该用户的Web页面
 * @author: cg
 * @create: 2022-03-27 15:43
 **/
public class PageMaker {
    /**
     * 防止外部new出PageMaker的实例
     */
    private PageMaker() {

    }

    /**
     * 生成欢迎页面
     *
     * @param mailAddr 邮件地址
     * @param filename 生成的html文件名
     */
    public static void makeWelcomePage(String mailAddr, String filename) {
        try {
            Properties mailProperties = Database.getProperties("src\\com\\cg\\design\\facade\\maildata");
            String username = mailProperties.getProperty(mailAddr);
            HtmlWriter writer = new HtmlWriter(new FileWriter(filename));
            writer.title("Welcome to " + username + "'s page!");
            writer.paragraph(username + " 欢迎来到 " + username + " 的主页。");
            writer.paragraph(" 等着你的邮件哦！ ");
            writer.mailto(mailAddr, username);
            writer.close();
            System.out.println(filename + " is created for " + mailAddr + " (" + username + ")");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成邮件地址超链接集合页面
     *
     * @param filename 生成的html文件名
     */
    public static void makeLinkPage(String filename) {
        try {
            HtmlWriter writer = new HtmlWriter(new FileWriter(filename));
            writer.title("Link page");
            Properties mailProperties = Database.getProperties("src\\com\\cg\\design\\facade\\maildata");
            Enumeration<?> names = mailProperties.propertyNames();
            while (names.hasMoreElements()) {
                String mailAddr = (String) names.nextElement();
                String username = mailProperties.getProperty(mailAddr, "(unknown)");
                writer.mailto(mailAddr, username);
            }
            writer.close();
            System.out.println(filename + " is created.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
